package de.clapp.musicbox.listAdapter;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import de.clapp.CLAppModel;
import de.clapp.R;
import de.clapp.Settings;
import de.clapp.data.Club;
import de.clapp.data.MusicItem;
import de.clapp.musicbox.MusicModel;
import de.clapp.musicbox.MusicVoteModel;
import de.clapp.toolkit.ImageManager;

public class TitleListAdapter extends ArrayAdapter<MusicItem> implements Filterable, MusicListAdapter
{
    private final LayoutInflater inflater;

    private ArrayList<MusicItem> workingList;

    private final ArrayList<MusicItem> originalList;

    private MusicFilter filter;

    private MusicModel model;

    private ListFragment listFragment;
    
    public static String SelectedTitle;

    public TitleListAdapter(Context context, ListFragment listFragment)
    {
        super(context, R.layout.music_entry_title);

        this.model = MusicModel.getI();
        this.model.addObserver(this);

        MusicVoteModel.getI().addObserver(this);
        this.listFragment = listFragment;
        this.originalList = this.model.getSongList();
        this.workingList = new ArrayList<MusicItem>(this.model.getSongList());
        this.filter = new MusicFilter();
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    public void setSelectedArtist(int index)
    {
        SelectedTitle = this.workingList.get(index).getTitel();
    }
    
    @Override
    public int getCount()
    {
        return this.workingList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.music_entry_title, parent, false);

            holder = new ViewHolder();

            holder.title = (TextView) convertView.findViewById(R.id.music_entry_title);
            holder.artist = (TextView) convertView.findViewById(R.id.music_entry_artist);
            holder.image = (ImageView) convertView.findViewById(R.id.music_entry_image);
            holder.votes = (ProgressBar) convertView.findViewById(R.id.music_entry_voteBar);
            holder.percent = (TextView) convertView.findViewById(R.id.articleName);
            convertView.setTag(holder);

        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        // Inhalt aus der Liste eintragen
        MusicItem item = this.workingList.get(position);
        holder.title.setText(item.getTitel());
        holder.artist.setText(item.getAlbum() + " - " + item.getInterpret()); 
        
        int votesCount = MusicVoteModel.votesCounter;
        if (votesCount > 0)
        {
            holder.votes.setMax(votesCount);
            holder.votes.setProgress(item.getVotes());
            holder.percent.setText(item.getVotes() + "/" + votesCount);
        }
        else
        {
            holder.votes.setProgress(0);
            holder.percent.setText(item.getVotes() + "/" + 0);
        }

        String tag = "MusicImages\\" + InterpretListAdapter.SelectedArtist + "-" + AlbumListAdapter.SelectedAlbum + ".png";
        holder.image.setTag(tag);
        Club club = CLAppModel.getInstance().getCurrentClub();
        ImageManager.getInstance().loadBitmap(tag, holder.image, 50, 50, club.getPort(), club.getIpAddress());
        return convertView;
    }

    /**
     * The Class ViewHolder.
     */
    private static class ViewHolder
    {
        /** The logo image view. */
        public ImageView image;

        /** The title text view. */
        public TextView title;

        /** The content text view. */
        public TextView artist;

        public ProgressBar votes;

        public TextView percent;
    }

    @Override
    public Filter getFilter()
    {
        return this.filter;
    }

    private class MusicFilter extends Filter
    {

        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {
            FilterResults filterResults = new FilterResults();
            ArrayList<MusicItem> results = new ArrayList<MusicItem>();
            if (constraint != null)
            {
                constraint = constraint.toString().toLowerCase();
                for (MusicItem item : originalList)
                {
                    if (item.getTitel().toLowerCase().contains(constraint))
                    {
                        results.add(item);
                    }
                }
                filterResults.values = results;
            }
            return filterResults;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results)
        {
            workingList = (ArrayList<MusicItem>) results.values;
            notifyDataSetChanged();
        }
    }

    public void votesChanged()
    {
        super.notifyDataSetChanged();
    }

    @Override
    public void dataChanged()
    {
        this.workingList = new ArrayList<MusicItem>(this.model.getSongList());
        super.notifyDataSetChanged();
        this.listFragment.setListShown(true);
    }
}
